package com.medical.repository;

import com.medical.entity.Appointment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * 专家门诊预约数据访问层
 */
@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {
    
    /**
     * 根据患者ID查询预约记录
     */
    List<Appointment> findByPatientIdOrderByCreateTimeDesc(Long patientId);
    
    /**
     * 根据医生ID查询预约记录
     */
    List<Appointment> findByDoctorIdOrderByCreateTimeDesc(Long doctorId);
    
    /**
     * 根据预约日期和医生ID查询预约记录
     */
    List<Appointment> findByAppointmentDateAndDoctorId(LocalDate appointmentDate, Long doctorId);
    
    /**
     * 根据状态查询预约记录
     */
    List<Appointment> findByStatusOrderByCreateTimeDesc(String status);
    
    /**
     * 查询指定日期和时段的预约数量
     */
    @Query("SELECT COUNT(a) FROM Appointment a WHERE a.appointmentDate = :date AND a.timeSlot = :timeSlot AND a.doctorId = :doctorId AND a.status IN ('PENDING', 'APPROVED')")
    Long countByDateAndTimeSlotAndDoctor(@Param("date") LocalDate date, @Param("timeSlot") String timeSlot, @Param("doctorId") Long doctorId);
    
    /**
     * 根据预约单号查询
     */
    Appointment findByAppointmentNo(String appointmentNo);
}
